let app = getApp()

Page({
  data: {
    // swipe的小点 默认true
    indicatorDots: true,
    // 滚动模式  
    vertical: false,
    // 自动播放吗
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 500,
    items: [],
    images: [
      'https://cn.bing.com/th?id=OIP.0qo0f2U26LzPd2nOu90HcAHaEo&pid=Api&rs=1',
      'http://www.guojicoffee.com/uploadfile/2015/0106/20150106030652882.jpg',
      'http://www.wall001.com/photograph/coffee/mxxx01/[wall001.com]_coffee_wallpaper_71091.jpg'
    ],
    currentTab: 0,
    ifIndex: true, //顶部导航栏是否处于index
    couponArray: [],
    foodArray: [],
    commentArray: [] //评论数组
  },
  // 页面加载完成之后，发送请求数据的好时机，它的大名叫！！！
  // 钩子 钩子 钩子 钩子 钩子
  onLoad: function() {
    var that = this;

    that.getComment();
    setInterval(function () {
      that.getComment();
      // console.log(2);
    }, 1000)
    that.getRecommend();
    that.getCoupon();

    that.setData({
      shop_message: {
        "name": "SCAU胖哥烧烤",
        "latitude": 23.12463,
        "longitude": 113.36199,
        "address": "广东省广州市华南农业大学数学与信息学院10楼",
        "photo": "http://jzskxh.com/uploads/allimg/161210/1-16121014515Q41.jpg",
      }
    })
  },
  //点击切换
  clickTab: function(e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (that.data.currentTab == 0) {
      that.setData({
        ifIndex: true
      })
    } else {
      that.setData({
        ifIndex: false
      })
    }
  },
  clickMap: function(e) { //获取店铺位置
    var that = this;
    wx.openLocation({
      latitude: that.data.shop_message.latitude,
      longitude: that.data.shop_message.longitude,
      name: that.data.shop_message.name,
      address: that.data.shop_message.address,
      scale: 18
    })
  },
  clickCoupon: function(e) { //用户领取优惠券
    var _couponSeriesId = e.currentTarget.dataset.couponid;
    var _ifToken = e.currentTarget.dataset.ifToken;
    var _couponidx = e.currentTarget.dataset.couponidx;
    wx.request({
      url: app.globalData.ip + '/api/coupon',
      method: "POST",
      header: {
        "X-Token": app.globalData.token
      },
      data: {
        couponSeriesId: _couponSeriesId
      },
      success: e => {
        console.log(e);
        var that = this;
        var code = e.data.code;
        var _couponArray = that.data.couponArray;
        _couponArray[_couponidx].ifToken = true;
        that.setData({
          couponArray: _couponArray
        })
        if (code == 500) {
          wx.showToast({
            title: '🙈优惠券已领取！',
            icon: "none",
            duration: 2000,
          })
        } else if (code == 200) {
          wx.showToast({
            title: '领取成功',
            icon: "success",
            duration: 2000
          })
        }
      }
    })
  },
  getCoupon: function() { //获取当前商家提供的优惠券
    var that = this;
    wx.request({
      url: app.globalData.ip + '/api/couponSeries',
      method: "GET",
      header: {
        "X-Token": app.globalData.token
      },
      success: e => {
        that.setData({
          couponArray: e.data.data
        })
      }
    })
  },
  getRecommend: function() { //获取当前商家的推荐菜
    var that = this;
    wx.request({
      url: app.globalData.ip + "/api/recommend/",
      method: "GET",
      success: e => {
        var _foodArray = e.data.data;
        that.setData({
          foodArray: _foodArray
        })
      }
    })
  },
  getComment: function() { //获取商家评价
    var that = this;
    wx.request({
      url: app.globalData.ip + '/api/comment/1/10',
      method: "GET",
      success: e => {
        var _commentArray = e.data.data;
        // console.log(e);
        for (var i in _commentArray) {
          _commentArray[i].time = app.timeFormat(_commentArray[i].time)
        }
        that.setData({
          commentArray: _commentArray
        })
      }
    })
  },
  putPraise: function(e) {
    var that = this;
    var _commentId = e.currentTarget.dataset.commentid;
    wx.request({
      url: app.globalData.ip + '/api/comment/praise/' + _commentId,
      method: "PUT",
      success: e => {
        that.onLoad();
        that.setData({
          ifIndex: false
        })
      }
    })
  },
  toDishesDetail: function(e) {
    var _dishesid = e.currentTarget.dataset.dishesid;
    wx.navigateTo({
      url: '../dishesDetail/dishesDetail?dishesid=' + _dishesid,
    })
  }
})